package com.D5Power.Editor.tools
{
	import com.D5Power.Editor.EditorWindow.CharacterLinker;
	import com.D5Power.net.DLoader;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	
	import org.aswing.JLabel;
	import org.aswing.JPanel;
	
	/**
	 * 单行显示器
	 */ 
	public class LineShower extends JPanel
	{
		/**
		 * 
		 */
		public var id:uint;
		
		/**
		 * 数据
		 */ 
		private var data:Array=[];
		
		private var lab:JLabel;
		
		private var padding:uint;
		
		private var box:CharacterLinker;
		
		
		
		public function LineShower(_id:uint,_box:CharacterLinker)
		{
			id = _id;
			box = _box;
			super();
			setup();
		}
		
		/**
		 * 获取数据
		 */ 
		public function get Data():Array
		{
			return data;
		}
		
		/**
		 * 加载某目录下的全部图片
		 */ 
		public function load(fileList:Array):void
		{
			var fs:FileStream = new FileStream();
			
			var key:uint = 0;
			for each(var f:File in fileList)
			{
				if(f==null) return;
				if(CharacterLinker.allowList.indexOf(f.name.substr(f.name.length-3))==-1) continue;
				var bytes:ByteArray = new ByteArray();
				
				fs.open(f,FileMode.READ);
				fs.readBytes(bytes,0,fs.bytesAvailable);
				fs.close();
				
				var loader:DLoader = new DLoader();
				loader.data = key;
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplate);
				loader.loadBytes(bytes);
				
				key++;
			}
			
			lab.setText("第"+id+"行，包含"+key+"张图片！");
		}
		
		private function onLoadComplate(e:Event):void
		{
			var target:DLoader = e.target.loader as DLoader;
			var img:Bitmap = target.content as Bitmap;
			
			if(!CharacterLinker.setSized) box.setImgSize(img.width,img.height);
			
			var bd:BitmapData = img.bitmapData.clone();
			img.bitmapData.dispose();
			img = null;
			data[target.data] = bd;
			target.unload();
			target.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoadComplate);
			
		}
		
		private function setup():void
		{
			lab = new JLabel('');
			graphics.beginFill(0,0);
			graphics.drawRect(0,0,200,20);
			
			x = CharacterLinker.padding;
			y = CharacterLinker.padding+(CharacterLinker.padding+CharacterLinker.h)*(id+1);
			
			box.shower.append(this);
			addEventListener(MouseEvent.CLICK,onDelete);
		}
		
		private function onDelete(e:Event):void
		{
			
		}
	}
}